home *** CD-ROM | disk | FTP | other *** search
- package symjava.sql;
-
- import java.util.Date;
-
- public class Timestamp extends Date {
- private int nanos;
-
- public Timestamp(int year, int month, int date, int hour, int minute, int second, int nano) {
- super(year, month, date, hour, minute, second);
- if (nano > 999999999) {
- throw new IllegalArgumentException("nano > 999999999");
- } else {
- this.nanos = nano;
- }
- }
-
- public Timestamp(long time) {
- super(time / 1000L * 1000L);
- this.nanos = (int)(time % 1000L * 1000000L);
- }
-
- public static Timestamp valueOf(String s) {
- int a_nanos = 0;
- int firstColon = 0;
- int secondColon = 0;
- int period = 0;
- if (s == null) {
- throw new IllegalArgumentException();
- } else {
- String trimmed_s = s.trim();
- int dividingSpace = s.indexOf(32);
- if (dividingSpace > 0) {
- String date_s = s.substring(0, dividingSpace);
- String time_s = s.substring(dividingSpace + 1);
- int firstDash = date_s.indexOf(45);
- int secondDash = date_s.indexOf(45, firstDash + 1);
- if (time_s == null) {
- throw new IllegalArgumentException();
- } else {
- firstColon = time_s.indexOf(58);
- secondColon = time_s.indexOf(58, firstColon + 1);
- period = time_s.indexOf(46, secondColon + 1);
- if (firstDash > 0 & secondDash > 0 & secondDash < date_s.length() - 1) {
- int year = Integer.parseInt(date_s.substring(0, firstDash)) - 1900;
- int month = Integer.parseInt(date_s.substring(firstDash + 1, secondDash)) - 1;
- int day = Integer.parseInt(date_s.substring(secondDash + 1));
- if (firstColon > 0 & secondColon > 0 & secondColon < time_s.length() - 1) {
- int hour = Integer.parseInt(time_s.substring(0, firstColon));
- int minute = Integer.parseInt(time_s.substring(firstColon + 1, secondColon));
- int second;
- if (period > 0 & period < time_s.length() - 1) {
- second = Integer.parseInt(time_s.substring(secondColon + 1, period));
- String nanos_s = time_s.substring(period);
- if (nanos_s.length() > 10) {
- throw new IllegalArgumentException();
- }
-
- a_nanos = (int)Math.round((double)Float.valueOf(nanos_s) * (double)1.0E9F);
- } else {
- if (period > 0) {
- throw new IllegalArgumentException();
- }
-
- second = Integer.parseInt(time_s.substring(secondColon + 1));
- }
-
- return new Timestamp(year, month, day, hour, minute, second, a_nanos);
- } else {
- throw new IllegalArgumentException();
- }
- } else {
- throw new IllegalArgumentException();
- }
- }
- } else {
- throw new IllegalArgumentException();
- }
- }
- }
-
- public String toString() {
- int year = super.getYear() + 1900;
- int month = super.getMonth() + 1;
- int day = super.getDate();
- int hour = super.getHours();
- int minute = super.getMinutes();
- int second = super.getSeconds();
- String yearString = "" + year;
- String monthString;
- if (month < 10) {
- monthString = "0" + month;
- } else {
- monthString = Integer.toString(month);
- }
-
- String dayString;
- if (day < 10) {
- dayString = "0" + day;
- } else {
- dayString = Integer.toString(day);
- }
-
- String hourString;
- if (hour < 10) {
- hourString = "0" + hour;
- } else {
- hourString = Integer.toString(hour);
- }
-
- String minuteString;
- if (minute < 10) {
- minuteString = "0" + minute;
- } else {
- minuteString = Integer.toString(minute);
- }
-
- String secondString;
- if (second < 10) {
- secondString = "0" + second;
- } else {
- secondString = Integer.toString(second);
- }
-
- String nanosString;
- if (this.nanos == 0) {
- nanosString = "";
- } else {
- nanosString = Integer.toString(this.nanos);
- char[] nanosChar = new char[nanosString.length()];
- nanosString.getChars(0, nanosString.length(), nanosChar, 0);
-
- int truncIndex;
- for(truncIndex = nanosString.length() - 1; nanosChar[truncIndex] == '0'; --truncIndex) {
- }
-
- nanosString = new String(nanosChar, 0, truncIndex + 1);
- }
-
- return yearString + "-" + monthString + "-" + dayString + " " + hourString + ":" + minuteString + ":" + secondString + "." + nanosString;
- }
-
- public int getNanos() {
- return this.nanos;
- }
-
- public void setNanos(int n) {
- if (n > 999999999) {
- throw new IllegalArgumentException("nano > 999999999");
- } else {
- this.nanos = n;
- }
- }
-
- public boolean equals(Timestamp ts) {
- if (super.equals(ts)) {
- return this.nanos == ts.nanos;
- } else {
- return false;
- }
- }
-
- public boolean before(Timestamp ts) {
- if (super.before(ts)) {
- return true;
- } else if (super.equals(ts)) {
- return this.nanos < ts.nanos;
- } else {
- return false;
- }
- }
-
- public boolean after(Timestamp ts) {
- if (super.after(ts)) {
- return true;
- } else if (super.equals(ts)) {
- return this.nanos > ts.nanos;
- } else {
- return false;
- }
- }
- }
-